package com.xph.core.common.json;

import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.wink.client.ClientConfig;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.json.JSONException;
import org.json.JSONObject;

public class RestClientUtil
{
    private static Logger log = Logger.getLogger(RestClientUtil.class);
    public final static String DEFAUT_CONTENTTYPE = "application/json";
    private static int corePoolSize = 50;
    private static int maximumPoolSize = 500;
    private static int keepAliveTime = 30;
    private static int restReadTimeout = 20000;
    private static ClientConfig config = new ClientConfig();
    static {
        config.connectTimeout(restReadTimeout);
        config.readTimeout(restReadTimeout);
    }

    public static ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1000), new ThreadPoolExecutor.DiscardOldestPolicy());

    public static JSONObject resourceGet(String url)
    {
        log.debug("rest url is..." + url);
        JSONObject obj = null;
        ClientResponse response = null ;
        org.apache.wink.client.RestClient restclient = null;
        try
        {
            restclient = new org.apache.wink.client.RestClient(config);
            Resource resource = restclient.resource(url);
            response = resource.contentType(DEFAUT_CONTENTTYPE).get();
            obj = response.getEntity(JSONObject.class);
        }catch (Exception ex){
            log.error(ex.getMessage());
        }finally{
            response = null ;
            restclient = null;
        }
        return obj;
    }

    public static JSONObject resourcePost(String url, JSONObject json)
    {
        log.debug("rest url is..." + url + json.toString());
        JSONObject obj = null;
        org.apache.wink.client.RestClient restclient = null;
        ClientResponse response = null;
        try
        {
            restclient = new org.apache.wink.client.RestClient(config);
            Resource resource = restclient.resource(url);
            response = resource.contentType(DEFAUT_CONTENTTYPE).accept(DEFAUT_CONTENTTYPE).post(json);
            obj = response.getEntity(JSONObject.class);
        }catch (Exception ex){
            log.error(ex.getMessage());
        }finally{
            response = null;
            restclient = null;
        }
        return obj;
    }


    public static void syncResourceGet(String url)
    {
        log.debug("sync rest url is..." + url);
        org.apache.wink.client.RestClient restclient = null;
        try
        {
            restclient = new org.apache.wink.client.RestClient(config);
            SyncJsonThread jsonThread = new SyncJsonThread(restclient, url, true, null);
            pool.execute(jsonThread);
        }
        catch (Exception ex)
        {
            log.error(ex.getMessage());
        }
        return;
    }

    public static void syncResourcePost(String url, JSONObject json)
    {
        log.debug("sync rest url is..." + url + json.toString());
        try
        {
            org.apache.wink.client.RestClient restclient = new org.apache.wink.client.RestClient(config);
            SyncJsonThread jsonThread = new SyncJsonThread(restclient, url, false, json);
            pool.execute(jsonThread);
        }
        catch (Exception ex)
        {
            log.error(ex.getMessage());
        }

        return;
    }

    public static void syncNoProxyResourcePost(String url, JSONObject json)
    {
        log.debug("syncNoProxy rest url is..." + url + json.toString());
        try
        {
            org.apache.wink.client.RestClient restclient = new org.apache.wink.client.RestClient();
            SyncJsonThread jsonThread = new SyncJsonThread(restclient, url, false, json);
            pool.execute(jsonThread);
        }
        catch (Exception ex)
        {
            log.error(ex.getMessage());
        }

        return;
    }

}
